package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ColorUtils;


/**
 * Created by 贺玉琨 on 2019/3/16.
 */
public class GradientRoundCornerButton extends RoundCornerButton {
    protected int secondaryBackColor;
    protected int secondaryBackColorPress;
    protected GradientDrawable.Orientation gradientOrientation;

    public GradientRoundCornerButton(Context context) {
        this(context, null);
    }

    public GradientRoundCornerButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientRoundCornerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBackground(attrs);
        refreshBackground();
    }

    protected void initBackground(AttributeSet attrs) {
        super.initBackground(attrs);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GradientRoundCornerButton);
        secondaryBackColor = a.getColor(R.styleable.GradientRoundCornerButton_secondaryBackColor, ColorUtils.getDarkerColor(backColor));
        secondaryBackColorPress = a.getColor(R.styleable.GradientRoundCornerButton_secondaryBackColorPress, ColorUtils.getDarkerColor(backColorPress));
        switch (a.getInt(R.styleable.GradientRoundCornerButton_gradientOrientation, 0)) {
            case 1:
                gradientOrientation = GradientDrawable.Orientation.TOP_BOTTOM;
                break;
            default:
                gradientOrientation = GradientDrawable.Orientation.LEFT_RIGHT;
        }
        a.recycle();
    }

    public GradientRoundCornerButton setBackColor(@ColorInt int backColor) {
        return setBackColor(backColor, ColorUtils.getDarkerColor(backColor));
    }

    public GradientRoundCornerButton setBackColor(@ColorInt int backColor, @ColorInt int backColorPress) {
        return setBackColor(backColor, backColorPress, ColorUtils.getDarkerColor(backColor), ColorUtils.getDarkerColor(backColorPress));
    }

    public GradientRoundCornerButton setBackColorWithSecondary(@ColorInt int backColor, @ColorInt int secondaryBackColor) {
        return setBackColor(backColor, ColorUtils.getDarkerColor(backColor), secondaryBackColor, ColorUtils.getDarkerColor(secondaryBackColor));
    }

    public GradientRoundCornerButton setBackColor(@ColorInt int backColor, @ColorInt int backColorPress, @ColorInt int secondaryBackColor, @ColorInt int secondaryBackColorPress) {
        this.backColor = backColor;
        this.backColorPress = backColorPress;
        this.secondaryBackColor = secondaryBackColor;
        this.secondaryBackColorPress = secondaryBackColorPress;
        refreshBackground();
        return this;
    }

    public GradientRoundCornerButton setSecondaryBackColor(@ColorInt int secondaryBackColor) {
        return setBackColor(backColor, backColorPress, secondaryBackColor, ColorUtils.getDarkerColor(secondaryBackColor));
    }

    protected void refreshBackground() {
        if (!isEnabled()) {
            super.refreshBackground();
            return;
        }
        if (withRipple) {
            setBackground(getNormalDrawable());
        } else {
            StateListDrawable drawable = new StateListDrawable();
            drawable.addState(new int[]{android.R.attr.state_pressed}, getPressDrawable());
            drawable.addState(new int[]{}, getNormalDrawable());
            setBackground(drawable);
        }
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            setTranslationZ(0);
//            setElevation(0);
//            //获取outLine，我们需要使用ViewoutLineProvider
//            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
//                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//                @Override
//                public void getOutline(View view, Outline outline) {
////                    //修改outLine的形状，这里是设置分别设置左上右下，以及Radius
////                    outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),30);
//                }
//            };
//            //将需要控件重写设置形状
//            setOutlineProvider(viewOutlineProvider);
//        }
    }

    protected Drawable getNormalDrawable() {
        float[] cornerRadii = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        GradientDrawable normalDrawable = new GradientDrawable();
        normalDrawable.setColors(new int[]{backColor, secondaryBackColor});
        normalDrawable.setCornerRadii(cornerRadii);
        normalDrawable.setOrientation(gradientOrientation);
        normalDrawable.setGradientRadius(0);
        normalDrawable.setStroke(borderWidth, borderColor);
        if (withRipple) {
            return new RippleDrawable(ColorStateList.valueOf(0x22000000), normalDrawable, null);
        }
        return normalDrawable;
    }

    protected Drawable getPressDrawable() {
        float[] cornerRadii = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        GradientDrawable pressDrawable = new GradientDrawable();
        pressDrawable.setColors(new int[]{backColorPress, secondaryBackColorPress});
        pressDrawable.setCornerRadii(cornerRadii);
        pressDrawable.setOrientation(gradientOrientation);
        pressDrawable.setGradientRadius(0);
        pressDrawable.setStroke(borderWidth, borderColor);
        if (withRipple) {
            return new RippleDrawable(ColorStateList.valueOf(0x22000000), pressDrawable, null);
        }
        return pressDrawable;
    }
}
